package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.unionpay.advisors.UnionpayAdvisorsData;
import com.ucredit.babel.common.model.unionpay.advisors.UnionpayAdvisorsUpRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 银联智策移动交易类变量
 *
 * @author renzhanzheng
 */

public class UnionpayAdvisorsUpParse  extends ParseBase{
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 银联智策移动交易类变量
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        UnionpayAdvisorsUpRecord obj = (UnionpayAdvisorsUpRecord)model;
        UnionpayAdvisorsData data = obj.getData() == null ? new UnionpayAdvisorsData() : obj.getData() ;
        JSONArray objects = data.getData() == null ? new JSONArray() : JSON.parseArray(data.getData());

        StringBuilder sb = getBaseInfo(obj, successful);
        //应答码
        sb.append(data.getErrorCode() == null ? "" : data.getErrorCode()).append(CRT001);
        //请求码
        sb.append(data.getRequestCode() == null ? "" : data.getRequestCode()).append(CRT001);
        //响应码
        sb.append(data.getResponseCode() == null ? "" : data.getResponseCode()).append(CRT001);

        StringBuilder txSb = getBaseInfo(obj);

        StringBuilder detailSb = new StringBuilder(500);
        int j = 0;
        for(int i = 0 ;i < objects.size();i++) {
            j++;
            JSONObject o = objects.getJSONObject(i);
            //序号
            detailSb.append( i+1).append(CRT001);
            //12个月内二维码最早交易日期
            detailSb.append(o.getString("YDZF001") == null ? "" : o.getString("YDZF001")).append(CRT001);
            //12个月内二维码最近一笔交易日期
            detailSb.append(o.getString("YDZF002") == null ? "" : o.getString("YDZF002")).append(CRT001);
            //12个月内二维码最近一笔失败交易日期
            detailSb.append(o.getString("YDZF003") == null ? "" : o.getString("YDZF003")).append(CRT001);
            //12个月内移动支付最早交易日期
            detailSb.append(o.getString("YDZF004") == null ? "" : o.getString("YDZF004")).append(CRT001);
            //12个月内移动支付最近一笔交易日期
            detailSb.append(o.getString("YDZF005") == null ? "" : o.getString("YDZF005")).append(CRT001);
            //12个月内移动支付最近一笔失败交易日期
            detailSb.append(o.getString("YDZF006") == null ? "" : o.getString("YDZF006")).append(CRT001);
            //1个月内移动支付成功交易强度
            detailSb.append(o.getString("YDZF007") == null ? "" : o.getString("YDZF007")).append(CRT001);
            //3个月内移动支付成功交易强度
            detailSb.append(o.getString("YDZF008") == null ? "" : o.getString("YDZF008")).append(CRT001);
            //6个月内移动支付成功交易强度
            detailSb.append(o.getString("YDZF009") == null ? "" : o.getString("YDZF009")).append(CRT001);
            //12个月内移动支付成功交易强度
            detailSb.append(o.getString("YDZF010") == null ? "" : o.getString("YDZF010")).append(CRT001);
            //1个月内移动支付成功交易笔数
            detailSb.append(o.getString("YDZF011") == null ? "" : o.getString("YDZF011")).append(CRT001);
            //3个月内移动支付成功交易笔数
            detailSb.append(o.getString("YDZF012") == null ? "" : o.getString("YDZF012")).append(CRT001);
            //6个月内移动支付成功交易笔数
            detailSb.append(o.getString("YDZF013") == null ? "" : o.getString("YDZF013")).append(CRT001);
            //12个月内移动支付成功交易笔数
            detailSb.append(o.getString("YDZF014") == null ? "" : o.getString("YDZF014")).append(CRT001);
            //1个月内移动支付成功交易天数
            detailSb.append(o.getString("YDZF015") == null ? "" : o.getString("YDZF015")).append(CRT001);
            //3个月内移动支付成功交易天数
            detailSb.append(o.getString("YDZF016") == null ? "" : o.getString("YDZF016")).append(CRT001);
            //6个月内移动支付成功交易天数
            detailSb.append(o.getString("YDZF017") == null ? "" : o.getString("YDZF017")).append(CRT001);
            //12个月内移动支付成功交易天数
            detailSb.append(o.getString("YDZF018") == null ? "" : o.getString("YDZF018")).append(CRT001);
            //1个月内二维码成功交易强度
            detailSb.append(o.getString("YDZF019") == null ? "" : o.getString("YDZF019")).append(CRT001);
            //3个月内二维码成功交易强度
            detailSb.append(o.getString("YDZF020") == null ? "" : o.getString("YDZF020")).append(CRT001);
            //6个月内二维码成功交易强度
            detailSb.append(o.getString("YDZF021") == null ? "" : o.getString("YDZF021")).append(CRT001);
            //12个月内二维码成功交易强度
            detailSb.append(o.getString("YDZF022") == null ? "" : o.getString("YDZF022")).append(CRT001);
            //1个月内二维码成功交易笔数
            detailSb.append(o.getString("YDZF023") == null ? "" : o.getString("YDZF023")).append(CRT001);
            //3个月内二维码成功交易笔数
            detailSb.append(o.getString("YDZF024") == null ? "" : o.getString("YDZF024")).append(CRT001);
            //6个月内二维码成功交易笔数
            detailSb.append(o.getString("YDZF025") == null ? "" : o.getString("YDZF025")).append(CRT001);
            //12个月内二维码成功交易笔数
            detailSb.append(o.getString("YDZF026") == null ? "" : o.getString("YDZF026")).append(CRT001);
            //1个月内二维码成功交易天数
            detailSb.append(o.getString("YDZF027") == null ? "" : o.getString("YDZF027")).append(CRT001);
            //3个月内二维码成功交易天数
            detailSb.append(o.getString("YDZF028") == null ? "" : o.getString("YDZF028")).append(CRT001);
            //6个月内二维码成功交易天数
            detailSb.append(o.getString("YDZF029") == null ? "" : o.getString("YDZF029")).append(CRT001);
            //12个月内二维码成功交易天数
            detailSb.append(o.getString("YDZF030") == null ? "" : o.getString("YDZF030")).append(CRT001);
            //1个月内移动支付失败交易强度
            detailSb.append(o.getString("YDZF031") == null ? "" : o.getString("YDZF031")).append(CRT001);
            //3个月内移动支付失败交易强度
            detailSb.append(o.getString("YDZF032") == null ? "" : o.getString("YDZF032")).append(CRT001);
            //6个月内移动支付失败交易强度
            detailSb.append(o.getString("YDZF033") == null ? "" : o.getString("YDZF033")).append(CRT001);
            //12个月内移动支付失败交易强度
            detailSb.append(o.getString("YDZF034") == null ? "" : o.getString("YDZF034")).append(CRT001);
            //1个月内移动支付失败交易笔数
            detailSb.append(o.getString("YDZF035") == null ? "" : o.getString("YDZF035")).append(CRT001);
            //3个月内移动支付失败交易笔数
            detailSb.append(o.getString("YDZF036") == null ? "" : o.getString("YDZF036")).append(CRT001);
            //6个月内移动支付失败交易笔数
            detailSb.append(o.getString("YDZF037") == null ? "" : o.getString("YDZF037")).append(CRT001);
            //12个月内移动支付失败交易笔数
            detailSb.append(o.getString("YDZF038") == null ? "" : o.getString("YDZF038")).append(CRT001);
            //1个月内移动支付失败交易天数
            detailSb.append(o.getString("YDZF039") == null ? "" : o.getString("YDZF039")).append(CRT001);
            //3个月内移动支付失败交易天数
            detailSb.append(o.getString("YDZF040") == null ? "" : o.getString("YDZF040")).append(CRT001);
            //6个月内移动支付失败交易天数
            detailSb.append(o.getString("YDZF041") == null ? "" : o.getString("YDZF041")).append(CRT001);
            //12个月内移动支付失败交易天数
            detailSb.append(o.getString("YDZF042") == null ? "" : o.getString("YDZF042")).append(CRT001);
            //1个月内二维码失败交易强度
            detailSb.append(o.getString("YDZF043") == null ? "" : o.getString("YDZF043")).append(CRT001);
            //3个月内二维码失败交易强度
            detailSb.append(o.getString("YDZF044") == null ? "" : o.getString("YDZF044")).append(CRT001);
            //6个月内二维码失败交易强度
            detailSb.append(o.getString("YDZF045") == null ? "" : o.getString("YDZF045")).append(CRT001);
            //12个月内二维码失败交易强度
            detailSb.append(o.getString("YDZF046") == null ? "" : o.getString("YDZF046")).append(CRT001);
            //1个月内二维码失败交易笔数
            detailSb.append(o.getString("YDZF047") == null ? "" : o.getString("YDZF047")).append(CRT001);
            //3个月内二维码失败交易笔数
            detailSb.append(o.getString("YDZF048") == null ? "" : o.getString("YDZF048")).append(CRT001);
            //6个月内二维码失败交易笔数
            detailSb.append(o.getString("YDZF049") == null ? "" : o.getString("YDZF049")).append(CRT001);
            //12个月内二维码失败交易笔数
            detailSb.append(o.getString("YDZF050") == null ? "" : o.getString("YDZF050")).append(CRT001);
            //1个月内二维码失败交易天数
            detailSb.append(o.getString("YDZF051") == null ? "" : o.getString("YDZF051")).append(CRT001);
            //3个月内二维码失败交易天数
            detailSb.append(o.getString("YDZF052") == null ? "" : o.getString("YDZF052")).append(CRT001);
            //6个月内二维码失败交易天数
            detailSb.append(o.getString("YDZF053") == null ? "" : o.getString("YDZF053")).append(CRT001);
            //12个月内二维码失败交易天数
            detailSb.append(o.getString("YDZF054") == null ? "" : o.getString("YDZF054")).append(CRT001);

            key.set(getKey());
            val.set((sb.toString() + detailSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        if (j == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_yinlianzhice_yidongjiaoyi_inc";

    }

    @Override
    public String getType() {
        return RecordType.UNIONPAY_ADVISORS_UP_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "银联智策移动交易类变量";
    }
}
